package com.yanqu.road.dao.impl.kuang.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.kuang.battle.CrossKuangTreasureData;
import com.yanqu.road.entity.kuang.battle.CrossKuangTreasureDetailData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossKuangTreasureDataDaoImpl extends TempDataObjectDao<CrossKuangTreasureData> {

    public static String getCrossKuangTreasureDataSql = "select * from `t_c_kuang_treasure_data` where `weekId`=? and `groupId`=? ";

    public Map<Integer, CrossKuangTreasureData> getCrossKuangTreasureDataMap(int weekId, int groupId) {
        Map<Integer, CrossKuangTreasureData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossKuangTreasureDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossKuangTreasureData parse = parse(rs);
                map.put(parse.getCampId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCrossKuangTreasureDataSql = "update `t_c_kuang_treasure_data` set round=?,specialPosition=?,treasureDetailDataMap=? where weekId=? and groupId=? and campId=?";
    @Override
    protected boolean updateData(CrossKuangTreasureData crossKuangTreasureData) {
        crossKuangTreasureData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureData.getSpecialPosition()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangTreasureData.getTreasureDetailDataMap())));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureData.getCampId()));
        if (getDb().executeNoneQuery(updateCrossKuangTreasureDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossKuangTreasureDataSql = "insert into `t_c_kuang_treasure_data`(weekId,groupId,campId,round,specialPosition,treasureDetailDataMap) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossKuangTreasureData crossKuangTreasureData) {
        crossKuangTreasureData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureData.getWeekId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureData.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossKuangTreasureData.getSpecialPosition()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossKuangTreasureData.getTreasureDetailDataMap())));
        if (getDb().executeNoneQuery(insertCrossKuangTreasureDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossKuangTreasureData parseData(ResultSet rs) throws SQLException {
        CrossKuangTreasureData crossKuangTreasureData = new CrossKuangTreasureData();
        crossKuangTreasureData.setWeekId(rs.getInt("weekId"));
        crossKuangTreasureData.setGroupId(rs.getInt("groupId"));
        crossKuangTreasureData.setCampId(rs.getInt("campId"));
        crossKuangTreasureData.setRound(rs.getInt("round"));
        crossKuangTreasureData.setSpecialPosition(rs.getInt("specialPosition"));
        String treasureDetailDataMap = rs.getString("treasureDetailDataMap");
        if (!StringUtils.isNullOrEmpty(treasureDetailDataMap)) {
            crossKuangTreasureData.setTreasureDetailDataMap(JSON.parseObject(treasureDetailDataMap, new TypeReference<Map<Integer, CrossKuangTreasureDetailData>>() {
            }));
        }
        return crossKuangTreasureData;
    }
}
